package com.jumi.microservice.material.controller;

import com.jumi.common.log.annotation.Log;
import com.jumi.common.log.enums.BusinessType;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.HttpStatus;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.material.domain.JmSupplier;
import com.jumi.microservice.material.dto.supplier.JmSupplierDelRequest;
import com.jumi.microservice.material.dto.supplier.JmSupplierFindAllRequest;
import com.jumi.microservice.material.dto.supplier.JmSupplierRequest;
import com.jumi.microservice.material.dto.supplier.JmSupplierStatusRequest;
import com.jumi.microservice.material.service.IJmSupplierService;
import com.jumi.microservice.material.service.JmSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/supplier")
@Api(tags = {"【基础资料】供应商信息接口"})
@Service
public class JmSupplierController extends BaseController implements IJmSupplierService {
    @Resource
    private JmSupplierService jmSupplierService;

    /*添数据*/
    @PostMapping("/save")
    @ApiOperation("添加供应商接口")
    @Log(title = "供应链管理-基础管理-供应商管理", businessType = BusinessType.INSERT)
    public ResponseResult save(JmSupplierRequest JmSupplierRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String createBy) {
        JmSupplier jmSupplier = new JmSupplier();
        BeanUtils.copyProperties(JmSupplierRequest, jmSupplier);
        jmSupplier.setCreateBy(createBy);
        jmSupplier.setUpdateBy(createBy);
        int get = jmSupplierService.save(jmSupplier);
        if (get == -1) {
            return ResponseResult.error(HttpStatus.CONFLICT, "已经存在相同的名称!");
        } else if ((get == 0)) {
            return ResponseResult.error(HttpStatus.CONFLICT, "添加失败!");
        } else {
            return ResponseResult.success(jmSupplier.getSupplierId());
        }
    }

    /*更新数据*/
    @PostMapping("/update")
    @ApiOperation("更新供应商接口")
    @Log(title = "供应链管理-基础管理-供应商管理", businessType = BusinessType.UPDATE)
    public ResponseResult update(JmSupplierRequest JmSupplierRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmSupplier jmSupplier = new JmSupplier();
        BeanUtils.copyProperties(JmSupplierRequest, jmSupplier);
        jmSupplier.setUpdateBy(updateBy);
        int update = jmSupplierService.update(jmSupplier);
        if (update == -1) {
            return ResponseResult.error(HttpStatus.CONFLICT, "已经存在相同的名称!");
        } else if ((update == 0)) {
            return ResponseResult.error(HttpStatus.CONFLICT, "更新失败!");
        } else {
            return ResponseResult.success(jmSupplier.getSupplierId());
        }
    }

    /*根据id删除指定数据（软删除）*/
    @PostMapping("/delete")
    @ApiOperation("删除指定供应商")
    @Log(title = "供应链管理-基础管理-供应商管理", businessType = BusinessType.DELETE)
    public ResponseResult delete(JmSupplierDelRequest jmSupplierDelRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmSupplier jmSupplier = new JmSupplier();
        BeanUtils.copyProperties(jmSupplierDelRequest, jmSupplier);
        jmSupplier.setUpdateBy(updateBy);
        int backInfo = jmSupplierService.deleteById(jmSupplier);
        return backInfo == 0 ? ResponseResult.error(HttpStatus.CONFLICT, "操作失败!") : ResponseResult.success(jmSupplier.getSupplierId());
    }

    /*根据条件查询所有数据*/
    @GetMapping("/findAll")
    @ApiOperation("查寻多条供应商接口")
    public TableDataInfo findAll(JmSupplierFindAllRequest JmSupplierFindAllRequest) {
        JmSupplier jmSupplier = new JmSupplier();
        BeanUtils.copyProperties(JmSupplierFindAllRequest, jmSupplier);
        startPage();
        List<JmSupplier> listAll = jmSupplierService.findAll(jmSupplier);
        return getDataTable(listAll);
    }

    /*根据条件查询所有数据*/
    @Override
    @GetMapping("/findOne")
    @ApiOperation("查寻单条供应商接口")
    @ApiImplicitParam(name = "id", value = "查询供应商信息的id")
    public ResponseResult findById(Long id) {
        JmSupplier listOne = jmSupplierService.findById(id);
        return ResponseResult.success(listOne);
    }

    /*列表中更新上下架状态*/
    @PostMapping("/statusById")
    @ApiOperation("供应商上下架接口")
    @Log(title = "供应链管理-基础管理-供应商管理", businessType = BusinessType.UPDATE)
    public ResponseResult statusById(@Validated JmSupplierStatusRequest jmSupplierStatusRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmSupplier jmSupplier = new JmSupplier();
        BeanUtils.copyProperties(jmSupplierStatusRequest, jmSupplier);
        jmSupplier.setUpdateBy(updateBy);
        int getNum = jmSupplierService.statusById(jmSupplier);
        return getNum == 0 ? ResponseResult.error(HttpStatus.CONFLICT, "操作失败!") : ResponseResult.success(jmSupplier.getSupplierId());
    }


    /**
     * supplierName在此处代指供应商厂家名称
     *
     * @param supplierName
     * @return
     */
    @Override
    public ResponseResult findAllByName(String supplierName) {
        JmSupplier jmSupplier = new JmSupplier();
        jmSupplier.setSupplierCompanyName(supplierName);
        jmSupplier.setStatus(1);
        List<JmSupplier> listAll = jmSupplierService.findAll(jmSupplier);
        return ResponseResult.success(listAll);
    }

    @Override
    public ResponseResult list() {
        List<JmSupplier> list = jmSupplierService.findAll(new JmSupplier());
        return ResponseResult.success(list);
    }

}
